<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function index(){
        $username = $_SESSION['username'];
    	$id = I('get.id',1);
    	$kind = M("article_kind");
    	$kinds = $kind -> select();
    	$indexs = M("article_content") -> where('kid='.$id) ->order('id asc')-> select();
        $this -> assign("kinds",$kinds);
        $this -> assign("indexs",$indexs);
        $this -> assign("username",$username);       
    	$this -> display();     
    }


    public function article(){
    	$id = I('get.id');
    	$kid = I('get.kid');
    	$title = M('article_content');
    	$content = $title ->where('id='.$id) -> select();
        //获取上一条文章信息
        $map['kid&id'] = array($kid,array('lt',$id),'_multi'=>true);
    	$res4 = M('article_content') -> where($map)->order('id desc')->limit(1)->find();
        //获取下一条文章信息
        $map1['kid&id'] = array($kid,array('gt',$id),'_multi'=>true);
        $res3 = M('article_content') -> where($map1)->limit(1)->find();
        //获取留言信息
        $map2['kid&flag'] =array($id,array('eq',1),'_multi'=>true);
        $messag = M('message') -> where($map2) -> select();
    	//插入留言     
    	$this->assign('conten',$content);
    	$this->assign('res4',$res4);  
        $this->assign('res3',$res3);
    	$this->assign('messag',$messag);   
    	$this->assign('kid',$kid);
        $this -> assign("id",$id);
    	$this->display();
    }



    public function message(){
        $id = I('post.id');
        $kid = I('post.kid');
        $username = I('post.username');
        $title1 = I('post.title1');
        $message = I('post.content');
        //插入留言
        if(!empty($username)){
         $data=array(
            'username'=>$username,
            'kid' =>$id,
            'time' =>time(),
            'message'=>$message,
            'title'=>$title1,
            'flag' => 0,
            );
         if(M('message')->add($data)){
            $this->success("留言成功,等待管理员审核");
         }else{
            $this->success("系统发生未知错误");
         }       
        }else{
            $this->error("你还没有登入");
        }       
    }



    public function login_do(){
        $username = I('post.username');
        $password = I('post.password');
        $map['username&password'] = array($username,$password,'_multi'=>true);
        if(M('user')->where($map)->find()){
         $_SESSION['username']=$username;
         $this->success("登入成功",U('Index/index'),3);
        }else{
         $this->success("用户名或密码错误",U('Index/login'),3);
        }
    }



    public function login(){
        $this->display();
    }



     public function logout(){
        session_destroy();
        $this->success("已安全退出",U('Index/login'));        
    }



     public function register(){
        $this->display();      
    }



    public function register_do(){
        $datas=M('user');
        $datas->create();
        $datas->regtime=time();
        $res = $datas->add();
        if($res){
            $this->success('注册成功',U('Index/login'));
        }else{
            $this->success('注册失败',U('Index/register'));
        }
    }


}